package com.jxc.controller.admin;

import com.jxc.entity.admin.Supplier;
import com.jxc.page.admin.Page;
import com.jxc.service.admin.ProductService;
import com.jxc.service.admin.SupplierService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Description: 供应商
 */
@Controller
@RequestMapping("/admin/supplier")
public class SupplierController {

    @Autowired
    private SupplierService supplierService;
    @Autowired
    private ProductService productService;

    /**
     * 进入供应商查询界面
     */
    @RequestMapping(value="/list",method= RequestMethod.GET)
    public ModelAndView list(ModelAndView model){
        model.setViewName("supplier/list");
        return model;
    }

    /**
     * 查询
     */
    @ResponseBody
    @RequestMapping(value = "list",method = RequestMethod.POST)
    public Map<String,Object> add(@RequestParam(value = "content",defaultValue = "") String content
    , Page page){
        HashMap<String, Object> map = new HashMap<>();
        map.put("content",content);
        Integer total = supplierService.getTotal(map);
        map.put("pageSize",page.getRows());
        map.put("offset",page.getOffset());
        List<Supplier> suppliers = supplierService.getList(map);

        HashMap<String, Object> ret = new HashMap<>();
        ret.put("total",total);
        ret.put("rows",suppliers);
        return ret;
    }

    /**
     * 添加一条记录
     */
    @ResponseBody
    @RequestMapping(value = "add",method = RequestMethod.POST)
    public Map<String,String> add(Supplier supplier){
        HashMap<String, String> map = new HashMap<>();
        if(supplier==null){
            map.put("type","error");
            map.put("msg","该信息为空，请填写再提交");
            return map;
        }
        if(StringUtils.isEmpty(supplier.getName())){
            map.put("type","error");
            map.put("msg","该供应商名称为空，请填写再提交");
            return map;
        }
        if(StringUtils.isEmpty(supplier.getAddress())){
            map.put("type","error");
            map.put("msg","该供应商地址为空，请填写再提交");
            return map;
        }
        if(StringUtils.isEmpty(supplier.getContactName())){
            map.put("type","error");
            map.put("msg","该供应商联系人为空，请填写再提交");
            return map;
        }
        if(StringUtils.isEmpty(supplier.getContactPhone())){
            map.put("type","error");
            map.put("msg","该联系电话为空，请填写再提交");
            return map;
        }
        if(StringUtils.isEmpty(supplier.getTel())){
            map.put("type","error");
            map.put("msg","该供应商电话为空，请填写再提交");
            return map;
        }
        if(supplierService.add(supplier) <= 0){
            map.put("type","error");
            map.put("msg","添加失败，请联系管理员");
            return map;
        }
        map.put("type","success");
        map.put("msg","添加成功！！！");
        return map;
    }

    /**
     * 修改一条记录
     */
    @ResponseBody
    @RequestMapping(value = "edit",method = RequestMethod.POST)
    public Map<String,String> edit(Supplier supplier){
        HashMap<String, String> map = new HashMap<>();
        if(supplier==null){
            map.put("type","error");
            map.put("msg","该信息为空，请填写再提交");
            return map;
        }
        if(StringUtils.isEmpty(supplier.getName())){
            map.put("type","error");
            map.put("msg","该供应商名称为空，请填写再提交");
            return map;
        }
        if(StringUtils.isEmpty(supplier.getAddress())){
            map.put("type","error");
            map.put("msg","该供应商地址为空，请填写再提交");
            return map;
        }
        if(StringUtils.isEmpty(supplier.getContactName())){
            map.put("type","error");
            map.put("msg","该供应商联系人为空，请填写再提交");
            return map;
        }
        if(StringUtils.isEmpty(supplier.getContactPhone())){
            map.put("type","error");
            map.put("msg","该联系电话为空，请填写再提交");
            return map;
        }
        if(StringUtils.isEmpty(supplier.getTel())){
            map.put("type","error");
            map.put("msg","该供应商电话为空，请填写再提交");
            return map;
        }
        if(supplierService.edit(supplier) <= 0){
            map.put("type","error");
            map.put("msg","修改失败，请联系管理员");
            return map;
        }
        map.put("type","success");
        map.put("msg","修改成功！！！");
        return map;
    }

    /**
     * 批量删除
     */
    @RequestMapping(value="delete",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, String> delete(@RequestParam(value = "ids") String ids){
        Map<String, String> ret = new HashMap<>();
        if(StringUtils.isEmpty(ids)){
            ret.put("type", "error");
            ret.put("msg", "选择要删除的数据！");
            return ret;
        }
        String[] split = new String[ids.length()] ;
        if(ids.contains(",")){
            ids = ids.substring(0,ids.length()-1);
            split = ids.split(",");
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (String id : split) {
            if(productService.getProductBySupplierId(id)!=null){
                Supplier supplier = supplierService.getSupplierById(id);
                stringBuilder.append(supplier.getName());
                stringBuilder.append(",");
            }
        }
        if(stringBuilder.length()>0){
            ret.put("type", "error");
            try {
                String msg = "删除失败，该["+new String(stringBuilder.toString().getBytes("iso8859-1"),"gbk")+"]供应商下还有商品!";
                ret.put("msg", new String(msg.getBytes("gbk"),"utf-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            return ret;
        }
        if(supplierService.deleteByIds(ids) <= 0){
            ret.put("type", "error");
            ret.put("msg", "删除失败，请联系管理员！");
            return ret;
        }
        ret.put("type", "success");
        ret.put("msg", "删除成功！");
        return ret;
    }

    public static void main(String[] args) {
        String ids = "4,5";
        String[] split = ids.split(",");
        for (String s : split) {
            System.out.println(s);
        }
    }

}
